﻿using System;
using System.Web;
using System.Net.Mail;

namespace MikeKappel.Com.CS.Cookies
{
    /// <summary>
    /// Tracking Cookie
    /// </summary>
    public static class Tracking
    {
        private const String COOKIE_NAME = "MikeKappel";
        private const String DATE_CREATED = "DC";
        private const String COOKIE_ID = "ID";
        private const String PAGE_VIEWS = "PV";
       
        public static Guid ID
        {
            get
            {
                return Get<Guid>(COOKIE_ID);
            }
            set
            {
                Set(COOKIE_ID, value);
            }
        }
        public static Int32 PageViews
        {
            get
            {
                return Get<Int32>(PAGE_VIEWS);
            }
            set
            {
                Set(PAGE_VIEWS,value);
            }
        }
        public static DateTime Created
        {
            get
            {
                return Get<DateTime>(DATE_CREATED);
            }
        }
        private static void Set(String name, Object value)
        {
            HttpCookie cookie = GetCookie();
            if (cookie == null)
                return;
            cookie[name] = (value == null) ? "" : HttpContext.Current.Server.HtmlDecode(value.ToString());
            HttpContext.Current.Response.Cookies.Set(cookie);
        }
        private static T Get<T>(String name)
        {
            HttpCookie cookie = GetCookie();
            if (cookie == null)
                return default(T);
            return Helper.ParseTo<T>(HttpContext.Current.Server.HtmlEncode(cookie[name] ?? ""));
        }
        private static HttpCookie GetCookie()
        {
            HttpCookie cookie;
            if ((HttpContext.Current.Response.Cookies[COOKIE_NAME] == null || String.IsNullOrEmpty(HttpContext.Current.Response.Cookies[COOKIE_NAME][DATE_CREATED]))
                && (HttpContext.Current.Request.Cookies[COOKIE_NAME] == null || String.IsNullOrEmpty(HttpContext.Current.Request.Cookies[COOKIE_NAME][DATE_CREATED])))
            {
                cookie = new HttpCookie(COOKIE_NAME);
                cookie.Name = COOKIE_NAME;
                cookie[COOKIE_ID] = Guid.NewGuid().ToString();
                cookie[PAGE_VIEWS] = "0";
                cookie[DATE_CREATED] = DateTime.Now.ToString();
                HttpContext.Current.Response.Cookies.Set(cookie);
            }
            if (String.IsNullOrEmpty(HttpContext.Current.Response.Cookies[COOKIE_NAME][DATE_CREATED]))
                cookie = HttpContext.Current.Request.Cookies[COOKIE_NAME];
            else
                cookie = HttpContext.Current.Response.Cookies[COOKIE_NAME];

            cookie.Expires = DateTime.Now.AddYears(1);
            cookie.Domain = Helper.Domain();
            return cookie;
        }
        public static void Cancel()
        {
            HttpCookie cookie = new HttpCookie(COOKIE_NAME);
            cookie.Expires = DateTime.Now.AddDays(-1);
            cookie.Domain = Helper.Domain();
            HttpContext.Current.Response.Cookies.Set(cookie);
        }
    }
}